package cn.bingoo.handy;

import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import uglify.UglifyJS;
import cn.bingoo.lang.Files;
import cn.bingoo.swing.utils.Swings;

public class UglifyJsFrame extends JFrame {
    private static final long serialVersionUID = 8093028000142386183L;
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UglifyJsFrame frame = new UglifyJsFrame();
                    frame.setVisible(true);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public UglifyJsFrame() {
        setTitle("Uglify JS(v1.26)");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 704, 444);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[] { 0, 0 };
        gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0, 0 };
        gbl_contentPane.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
        gbl_contentPane.rowWeights = new double[] { 1.0, 0.0, 1.0, 0.0, Double.MIN_VALUE };
        contentPane.setLayout(gbl_contentPane);

        JScrollPane scrollPane = new JScrollPane();
        GridBagConstraints gbc_scrollPane = new GridBagConstraints();
        gbc_scrollPane.fill = GridBagConstraints.BOTH;
        gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
        gbc_scrollPane.gridx = 0;
        gbc_scrollPane.gridy = 0;
        contentPane.add(scrollPane, gbc_scrollPane);

        final JTextArea textSrc = new JTextArea();
        textSrc.setLineWrap(true);
        scrollPane.setViewportView(textSrc);

        JPanel panel = new JPanel();
        GridBagConstraints gbc_panel = new GridBagConstraints();
        gbc_panel.insets = new Insets(0, 0, 5, 0);
        gbc_panel.fill = GridBagConstraints.BOTH;
        gbc_panel.gridx = 0;
        gbc_panel.gridy = 1;
        contentPane.add(panel, gbc_panel);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[] { 0, 0, 0, 0, 0, 0 };
        gbl_panel.rowHeights = new int[] { 0, 0 };
        gbl_panel.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE };
        gbl_panel.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
        panel.setLayout(gbl_panel);

        JButton btnCompress = new JButton("");
        btnCompress.setToolTipText("Compress");
        btnCompress.setIcon(new ImageIcon(UglifyJsFrame.class.getResource("/ico/compress.png")));

        GridBagConstraints gbc_btnCompress = new GridBagConstraints();
        gbc_btnCompress.insets = new Insets(0, 0, 0, 5);
        gbc_btnCompress.gridx = 0;
        gbc_btnCompress.gridy = 0;
        panel.add(btnCompress, gbc_btnCompress);

        final JLabel lblSourceResult = new JLabel("Source:0 Result:0 Saved:0 Ratio:0");
        GridBagConstraints gbc_lblSourceResult = new GridBagConstraints();
        gbc_lblSourceResult.insets = new Insets(0, 0, 0, 5);
        gbc_lblSourceResult.gridx = 1;
        gbc_lblSourceResult.gridy = 0;
        panel.add(lblSourceResult, gbc_lblSourceResult);

        JButton btnRunCode_1 = new JButton("");
        btnRunCode_1.setToolTipText("Run Code");
        btnRunCode_1.setIcon(new ImageIcon(UglifyJsFrame.class.getResource("/ico/running.png")));
        btnRunCode_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    File tempFile = File.createTempFile("uglifyjs_output", ".html");
                    Files.writeStringToFile(tempFile, "<script>" + textSrc.getText() + "</script>");
                    Desktop.getDesktop().open(tempFile);
                }
                catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });

        JButton btnPaste = new JButton("");
        btnPaste.setIcon(new ImageIcon(UglifyJsFrame.class.getResource("/ico/paste.png")));
        btnPaste.setToolTipText("Paste");
        btnPaste.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Swings.fromClipBoard(textSrc);
            }
        });
        GridBagConstraints gbc_btnPaste = new GridBagConstraints();
        gbc_btnPaste.anchor = GridBagConstraints.EAST;
        gbc_btnPaste.insets = new Insets(0, 0, 0, 5);
        gbc_btnPaste.gridx = 2;
        gbc_btnPaste.gridy = 0;
        panel.add(btnPaste, gbc_btnPaste);

        JButton btnClean = new JButton("");
        btnClean.setToolTipText("Clean");
        btnClean.setIcon(new ImageIcon(UglifyJsFrame.class.getResource("/ico/clear.png")));
        btnClean.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Swings.clearText(textSrc);
            }
        });
        GridBagConstraints gbc_btnClean = new GridBagConstraints();
        gbc_btnClean.insets = new Insets(0, 0, 0, 5);
        gbc_btnClean.gridx = 3;
        gbc_btnClean.gridy = 0;
        panel.add(btnClean, gbc_btnClean);
        GridBagConstraints gbc_btnRunCode_1 = new GridBagConstraints();
        gbc_btnRunCode_1.anchor = GridBagConstraints.EAST;
        gbc_btnRunCode_1.gridx = 4;
        gbc_btnRunCode_1.gridy = 0;
        panel.add(btnRunCode_1, gbc_btnRunCode_1);

        JScrollPane scrollPane_1 = new JScrollPane();
        GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
        gbc_scrollPane_1.insets = new Insets(0, 0, 5, 0);
        gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
        gbc_scrollPane_1.gridx = 0;
        gbc_scrollPane_1.gridy = 2;
        contentPane.add(scrollPane_1, gbc_scrollPane_1);

        final JTextArea textResult = new JTextArea();
        textResult.setLineWrap(true);
        scrollPane_1.setViewportView(textResult);

        JPanel panel_2 = new JPanel();
        GridBagConstraints gbc_panel_2 = new GridBagConstraints();
        gbc_panel_2.fill = GridBagConstraints.BOTH;
        gbc_panel_2.gridx = 0;
        gbc_panel_2.gridy = 3;
        contentPane.add(panel_2, gbc_panel_2);
        GridBagLayout gbl_panel_2 = new GridBagLayout();
        gbl_panel_2.columnWidths = new int[] { 0, 0, 0, 0, 0 };
        gbl_panel_2.rowHeights = new int[] { 0, 0 };
        gbl_panel_2.columnWeights = new double[] { 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE };
        gbl_panel_2.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
        panel_2.setLayout(gbl_panel_2);

        JButton btnRunCode = new JButton("");
        btnRunCode.setIcon(new ImageIcon(UglifyJsFrame.class.getResource("/ico/running.png")));
        btnRunCode.setToolTipText("Run Code");
        btnRunCode.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    File tempFile = File.createTempFile("uglifyjs_output", ".html");
                    Files.writeStringToFile(tempFile, "<script>" + textResult.getText() + "</script>");
                    Desktop.getDesktop().open(tempFile);
                }
                catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });

        JButton btnCopy = new JButton("");
        btnCopy.setIcon(new ImageIcon(UglifyJsFrame.class.getResource("/ico/copy.png")));
        btnCopy.setToolTipText("Copy");
        btnCopy.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Swings.toClipBoard(textResult);
            }
        });
        GridBagConstraints gbc_btnCopy = new GridBagConstraints();
        gbc_btnCopy.anchor = GridBagConstraints.EAST;
        gbc_btnCopy.insets = new Insets(0, 0, 0, 5);
        gbc_btnCopy.gridx = 1;
        gbc_btnCopy.gridy = 0;
        panel_2.add(btnCopy, gbc_btnCopy);

        JButton btnClean_1 = new JButton("");
        btnClean_1.setIcon(new ImageIcon(UglifyJsFrame.class.getResource("/ico/clear.png")));
        btnClean_1.setToolTipText("Clean");
        btnClean_1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Swings.clearText(textResult);
            }
        });
        GridBagConstraints gbc_btnClean_1 = new GridBagConstraints();
        gbc_btnClean_1.insets = new Insets(0, 0, 0, 5);
        gbc_btnClean_1.gridx = 2;
        gbc_btnClean_1.gridy = 0;
        panel_2.add(btnClean_1, gbc_btnClean_1);
        GridBagConstraints gbc_btnRunCode = new GridBagConstraints();
        gbc_btnRunCode.anchor = GridBagConstraints.EAST;
        gbc_btnRunCode.gridx = 3;
        gbc_btnRunCode.gridy = 0;
        panel_2.add(btnRunCode, gbc_btnRunCode);

        btnCompress.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    lblSourceResult.setText("Source:0 Result:0 Saved:0 Ratio:0");
                    File tempFile = File.createTempFile("uglifyjs_input", ".js");
                    Files.writeStringToFile(tempFile, textSrc.getText());

                    String inputFileName = tempFile.getCanonicalPath();

                    String uglify = new UglifyJS().uglify(new String[] { inputFileName });
                    textResult.setText(uglify);

                    int srcLen = textSrc.getText().length();
                    int retLen = uglify.length();
                    lblSourceResult.setText(String.format("Source:%d Result:%d Saved:%d Ratio:%.2f%%",
                            srcLen, retLen, srcLen - retLen, retLen * 100.0 / srcLen));
                }
                catch (Exception e1) {
                    e1.printStackTrace();
                    textResult.setText("Exception!" + e1.toString());
                }
            }
        });
    }
}
